
#ifndef ROBBO_CONTO
#define ROBBO_CONTO
#include "RobboLito.h"

#include "Robbo_conto.c"
#include "white.h"
#else
#include "black.h"
#endif

static int
MY_COUNT_FLEE (typePOS *POSITION, uint64 * ep)
{
  int king, count, pi, sq;
  uint64 att, ip, T, flee, U;
  *ep = 0;
  king = MyKingSq;
  att = MyKingCheck;
  sq = BSF (att);
  pi = POSITION->sq[sq];
  flee = (~OppAttacked) &
    (((pi == EnumOppP || pi == EnumOppK) ? AttK[king] : 0) |
     Evade (king, sq)) & (~POSITION->OccupiedBW);
  BitClear (sq, att);
  if (att)
    {
      sq = BSF (att);
      pi = POSITION->sq[sq];
      BitClear (sq, att);
      if (att)
	{
	  *ep = -1;
	  return 0;
	}
      return
	POPCNT (flee & (((pi == EnumOppP || pi == EnumOppK) ? AttK[king] : 0) |
			Evade (king, sq)));
    }
  count = POPCNT (flee);
  ip = InterPose (king, sq) & ~POSITION->OccupiedBW;
  if (!ip)
    return count;
  T = BitboardMyP & ~BITBOARD_SEVENTH_RANK &
    BACK_SHIFT ((ip & OppOccupied) ^ ip) & ~OppXRAY;
  count += POPCNT (T);
  T = BitboardMyP & BACK_SHIFT2 ((ip & OppOccupied) ^ ip) &
    SECOND_RANK & BACK_SHIFT (~POSITION->OccupiedBW) & ~OppXRAY;
  T = FORWARD_SHIFT (FORWARD_SHIFT (T));
  count += POPCNT (T);
  *ep = T & (((BitboardOppP & FOURTH_RANK_BITBOARD) << 1) |
	     ((BitboardOppP & FOURTH_RANK_BITBOARD) >> 1));
  for (U = BitboardMyN & ~OppXRAY; U; BitClear (sq, U))
    count += POPCNT (AttN[BSF (U)] & ip);
  for (U = BitboardMyB & ~OppXRAY; U; BitClear (sq, U))
    count += POPCNT (AttB (BSF (U)) & ip);
  for (U = BitboardMyR & ~OppXRAY; U; BitClear (sq, U))
    count += POPCNT (AttR (BSF (U)) & ip);
  for (U = BitboardMyQ & ~OppXRAY; U; BitClear (sq, U))
    count += POPCNT (AttQ (BSF (U)) & ip);
  return count;
}

int
MY_COUNT (typePOS *POSITION, uint64 * ep)
{
  uint64 empty = ~POSITION->OccupiedBW, U, A, T;
  int b, count = 0;
  Mobility (POSITION);
  if (IN_CHECK)
    return MY_COUNT_FLEE (POSITION, ep);
  for (U = BitboardMyN; U; BitClear (b, U))
    {
      b = BSF (U);
      if (OppXRAY & SqSet[b])
	continue;
      A = AttN[b] & empty;
      count += POPCNT (A);
    }
  for (U = BitboardMyB; U; BitClear (b, U))
    {
      b = BSF (U);
      A = AttB (b) & empty;
      if (OppXRAY & SqSet[b])
	A &= InterPose (MyKingSq, OppXrayTable[b]);
      count += POPCNT (A);
    }
  for (U = BitboardMyR; U; BitClear (b, U))
    {
      b = BSF (U);
      A = AttR (b) & empty;
      if (OppXRAY & SqSet[b])
	A &= InterPose (MyKingSq, OppXrayTable[b]);
      count += POPCNT (A);
    }
  for (U = BitboardMyQ; U; BitClear (b, U))
    {
      b = BSF (U);
      A = AttQ (b) & empty;
      if (OppXRAY & SqSet[b])
	A &= InterPose (MyKingSq, OppXrayTable[b]);
      count += POPCNT (A);
    }
  count += POPCNT (AttK[MyKingSq] & empty & ~OppAttacked);
  count += POPCNT (BitboardMyP & (~BITBOARD_SEVENTH_RANK) &
		   BACK_SHIFT (empty) & (FileArray[FILE (MyKingSq)] | ~OppXRAY));
  T = BitboardMyP & SECOND_RANK & BACK_SHIFT (empty) &
    BACK_SHIFT2 (empty) & (FileArray[FILE (MyKingSq)] | ~OppXRAY);
  T = FORWARD_SHIFT (FORWARD_SHIFT (T));
  count += POPCNT (T);
  *ep = T & (((BitboardOppP & FOURTH_RANK_BITBOARD) << 1) |
	     ((BitboardOppP & FOURTH_RANK_BITBOARD) >> 1));
  return count;
}
